home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / fmmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  5.3 KB  |  301 lines

  1. /* Font Manager module */
  2.  
  3. #include "Python.h"
  4.  
  5. #include <gl.h>
  6. #include <device.h>
  7. #include <fmclient.h>
  8.  
  9.  
  10. /* Font Handle object implementation */
  11.  
  12. typedef struct {
  13.     PyObject_HEAD
  14.     fmfonthandle fh_fh;
  15. } fhobject;
  16.  
  17. staticforward PyTypeObject Fhtype;
  18.  
  19. #define is_fhobject(v)        ((v)->ob_type == &Fhtype)
  20.  
  21. static PyObject *
  22. newfhobject(fh)
  23.     fmfonthandle fh;
  24. {
  25.     fhobject *fhp;
  26.     if (fh == NULL) {
  27.         PyErr_SetString(PyExc_RuntimeError,
  28.                 "error creating new font handle");
  29.         return NULL;
  30.     }
  31.     fhp = PyObject_New(fhobject, &Fhtype);
  32.     if (fhp == NULL)
  33.         return NULL;
  34.     fhp->fh_fh = fh;
  35.     return (PyObject *)fhp;
  36. }
  37.  
  38. /* Font Handle methods */
  39.  
  40. static PyObject *
  41. fh_scalefont(self, args)
  42.     fhobject *self;
  43. PyObject *args;
  44. {
  45.     double size;
  46.     if (!PyArg_Parse(args, "d", &size))
  47.         return NULL;
  48.     return newfhobject(fmscalefont(self->fh_fh, size));
  49. }
  50.  
  51. /* XXX fmmakefont */
  52.  
  53. static PyObject *
  54. fh_setfont(self, args)
  55.     fhobject *self;
  56. PyObject *args;
  57. {
  58.     if (!PyArg_NoArgs(args))
  59.         return NULL;
  60.     fmsetfont(self->fh_fh);
  61.     Py_INCREF(Py_None);
  62.     return Py_None;
  63. }
  64.  
  65. static PyObject *
  66. fh_getfontname(self, args)
  67.     fhobject *self;
  68. PyObject *args;
  69. {
  70.     char fontname[256];
  71.     int len;
  72.     if (!PyArg_NoArgs(args))
  73.         return NULL;
  74.     len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
  75.     if (len < 0) {
  76.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
  77.         return NULL;
  78.     }
  79.     return PyString_FromStringAndSize(fontname, len);
  80. }
  81.  
  82. static PyObject *
  83. fh_getcomment(self, args)
  84.     fhobject *self;
  85. PyObject *args;
  86. {
  87.     char comment[256];
  88.     int len;
  89.     if (!PyArg_NoArgs(args))
  90.         return NULL;
  91.     len = fmgetcomment(self->fh_fh, sizeof comment, comment);
  92.     if (len < 0) {
  93.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
  94.         return NULL;
  95.     }
  96.     return PyString_FromStringAndSize(comment, len);
  97. }
  98.  
  99. static PyObject *
  100. fh_getfontinfo(self, args)
  101.     fhobject *self;
  102. PyObject *args;
  103. {
  104.     fmfontinfo info;
  105.     if (!PyArg_NoArgs(args))
  106.         return NULL;
  107.     if (fmgetfontinfo(self->fh_fh, &info) < 0) {
  108.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
  109.         return NULL;
  110.     }
  111.     return Py_BuildValue("(llllllll)",
  112.                  info.printermatched,
  113.                  info.fixed_width,
  114.                  info.xorig,
  115.                  info.yorig,
  116.                  info.xsize,
  117.                  info.ysize,
  118.                  info.height,
  119.                  info.nglyphs);
  120. }
  121.  
  122. #if 0
  123. static PyObject *
  124. fh_getwholemetrics(self, args)
  125.     fhobject *self;
  126. PyObject *args;
  127. {
  128. }
  129. #endif
  130.  
  131. static PyObject *
  132. fh_getstrwidth(self, args)
  133.     fhobject *self;
  134. PyObject *args;
  135. {
  136.     char *str;
  137.     if (!PyArg_Parse(args, "s", &str))
  138.         return NULL;
  139.     return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
  140. }
  141.  
  142. static PyMethodDef fh_methods[] = {
  143.     {"scalefont",    (PyCFunction)fh_scalefont},
  144.     {"setfont",    (PyCFunction)fh_setfont},
  145.     {"getfontname",    (PyCFunction)fh_getfontname},
  146.     {"getcomment",    (PyCFunction)fh_getcomment},
  147.     {"getfontinfo",    (PyCFunction)fh_getfontinfo},
  148. #if 0
  149.     {"getwholemetrics",    (PyCFunction)fh_getwholemetrics},
  150. #endif
  151.     {"getstrwidth",    (PyCFunction)fh_getstrwidth},
  152.     {NULL,        NULL}        /* sentinel */
  153. };
  154.  
  155. static PyObject *
  156. fh_getattr(fhp, name)
  157.     fhobject *fhp;
  158. char *name;
  159. {
  160.     return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
  161. }
  162.  
  163. static void
  164. fh_dealloc(fhp)
  165.     fhobject *fhp;
  166. {
  167.     fmfreefont(fhp->fh_fh);
  168.     PyObject_Del(fhp);
  169. }
  170.  
  171. static PyTypeObject Fhtype = {
  172.     PyObject_HEAD_INIT(&PyType_Type)
  173.     0,                /*ob_size*/
  174.     "font handle",            /*tp_name*/
  175.     sizeof(fhobject),        /*tp_size*/
  176.     0,                /*tp_itemsize*/
  177.     /* methods */
  178.     (destructor)fh_dealloc,        /*tp_dealloc*/
  179.     0,                /*tp_print*/
  180.     (getattrfunc)fh_getattr,    /*tp_getattr*/
  181.     0,                /*tp_setattr*/
  182.     0,                /*tp_compare*/
  183.     0,                /*tp_repr*/
  184. };
  185.  
  186.  
  187. /* Font Manager functions */
  188.  
  189. static PyObject *
  190. fm_init(self, args)
  191.     PyObject *self, *args;
  192. {
  193.     if (!PyArg_NoArgs(args))
  194.         return NULL;
  195.     fminit();
  196.     Py_INCREF(Py_None);
  197.     return Py_None;
  198. }
  199.  
  200. static PyObject *
  201. fm_findfont(self, args)
  202.     PyObject *self, *args;
  203. {
  204.     char *str;
  205.     if (!PyArg_Parse(args, "s", &str))
  206.         return NULL;
  207.     return newfhobject(fmfindfont(str));
  208. }
  209.  
  210. static PyObject *
  211. fm_prstr(self, args)
  212.     PyObject *self, *args;
  213. {
  214.     char *str;
  215.     if (!PyArg_Parse(args, "s", &str))
  216.         return NULL;
  217.     fmprstr(str);
  218.     Py_INCREF(Py_None);
  219.     return Py_None;
  220. }
  221.  
  222. /* XXX This uses a global variable as temporary! Not re-entrant! */
  223.  
  224. static PyObject *fontlist;
  225.  
  226. static void
  227. clientproc(fontname)
  228.     char *fontname;
  229. {
  230.     int err;
  231.     PyObject *v;
  232.     if (fontlist == NULL)
  233.         return;
  234.     v = PyString_FromString(fontname);
  235.     if (v == NULL)
  236.         err = -1;
  237.     else {
  238.         err = PyList_Append(fontlist, v);
  239.         Py_DECREF(v);
  240.     }
  241.     if (err != 0) {
  242.         Py_DECREF(fontlist);
  243.         fontlist = NULL;
  244.     }
  245. }
  246.  
  247. static PyObject *
  248. fm_enumerate(self, args)
  249.     PyObject *self, *args;
  250. {
  251.     PyObject *res;
  252.     if (!PyArg_NoArgs(args))
  253.         return NULL;
  254.     fontlist = PyList_New(0);
  255.     if (fontlist == NULL)
  256.         return NULL;
  257.     fmenumerate(clientproc);
  258.     res = fontlist;
  259.     fontlist = NULL;
  260.     return res;
  261. }
  262.  
  263. static PyObject *
  264. fm_setpath(self, args)
  265.     PyObject *self, *args;
  266. {
  267.     char *str;
  268.     if (!PyArg_Parse(args, "s", &str))
  269.         return NULL;
  270.     fmsetpath(str);
  271.     Py_INCREF(Py_None);
  272.     return Py_None;
  273. }
  274.  
  275. static PyObject *
  276. fm_fontpath(self, args)
  277.     PyObject *self, *args;
  278. {
  279.     if (!PyArg_NoArgs(args))
  280.         return NULL;
  281.     return PyString_FromString(fmfontpath());
  282. }
  283.  
  284. static PyMethodDef fm_methods[] = {
  285.     {"init",    fm_init},
  286.     {"findfont",    fm_findfont},
  287.     {"enumerate",    fm_enumerate},
  288.     {"prstr",    fm_prstr},
  289.     {"setpath",    fm_setpath},
  290.     {"fontpath",    fm_fontpath},
  291.     {NULL,        NULL}        /* sentinel */
  292. };
  293.  
  294.  
  295. void
  296. initfm()
  297. {
  298.     Py_InitModule("fm", fm_methods);
  299.     fminit();
  300. }
  301.